package com.erli.exchange.common.sg.mapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementRefs;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlRootElement;

import com.erli.exchange.core.util.JAXBUtil;

import lombok.Setter;

//<insert id="insert" useGeneratedKeys="true" keyProperty="id">
//insert into
//<include refid="BASE_TABLE"/>
//<trim prefix = "(" suffix=")" suffixOverrides=",">
//    <if test="userName != null and userName != '' " >
//        user_name,
//    </if>
//    <if test="userNameCn != null and userNameCn != '' ">
//        user_name_cn,
//    </if>
//    <if test="email != null and email != '' ">
//        email,
//    </if>
//    <if test="department != null and department != '' ">
//        department,
//    </if>
//    <if test="createTime != null ">
//        create_time,
//    </if>
//    <if test="enabled != '' and enabled != null ">
//        enabled,
//    </if>
//</trim>
//values
//<trim prefix = "(" suffix=")" suffixOverrides=",">
//    <if test="userName != null and userName != '' " >
//        #{userName},
//    </if>
//    <if test="userNameCn != null and userNameCn != '' ">
//        #{userNameCn},
//    </if>
//    <if test="email != null and email != '' ">
//        #{email},
//    </if>
//    <if test="department != null and department != '' ">
//        #{department},
//    </if>
//    <if test="createTime != null ">
//        #{createTime},
//    </if>
//    <if test="enabled != '' and enabled != null ">
//        #{enabled},
//    </if>
//</trim>
//</insert>
@Setter
@XmlRootElement(name = "insert")
public class Insert {

	@XmlAttribute
	private String id;
	@XmlAttribute
	private String useGeneratedKeys;
	@XmlAttribute
	private String keyProperty;
	
	@XmlElementRefs(value = { 
			@XmlElementRef(name = "include", type = Include.class),
			@XmlElementRef(name = "trim", type = Trim.class)
			})
	@XmlMixed
	private List<Object> list;
	
	public static void main(String[] args) throws JAXBException, IOException {
		Insert insert = new Insert();
		insert.setId("insert");
		insert.setUseGeneratedKeys("true");
		insert.setKeyProperty("id");
		
		List<Object> list = new ArrayList<Object>();
		
		Include include1 = new Include();
		include1.setRefid("BASE_TABLE");
		
		Trim trim1 = new Trim();
		List<If> ifList1 = new ArrayList<>();
		If if1 = new If();
		if1.setTest("userName != null and userName != '' ");
		if1.setValue("user_name,");
		If if2 = new If();
		if2.setTest("userNameCn != null and userNameCn != ''");
		if2.setValue("user_name_cn,");
		ifList1.add(if1);
		ifList1.add(if2);
		trim1.setIfList(ifList1);
		
		
		Trim trim2 = new Trim();
		List<If> ifList2 = new ArrayList<>();
		If if3 = new If();
		if3.setTest("userName != null and userName != '' ");
		if3.setValue("#{userName},");
		If if4 = new If();
		if4.setTest("userNameCn != null and userNameCn != ''");
		if4.setValue("#{userNameCn},");
		ifList2.add(if3);
		ifList2.add(if4);
		trim2.setIfList(ifList2);
		
		list.add("insert into");
		list.add(include1);
		list.add(trim1);
		list.add("values");
		list.add(trim2);
		
		insert.setList(list);
		
		System.out.println(JAXBUtil.toXML(insert));
	}
}
